home *** CD-ROM | disk | FTP | other *** search
/ Future Workshop / Future Workshop.iso / multimed / qtw111 / pviewer / common.c < prev    next >
C/C++ Source or Header  |  1994-01-11  |  13KB  |  413 lines

  1.  
  2. // ---------------------------------------------------------------------
  3. //
  4. // Common.c - Common Routines - QuickTime for Windows
  5. //
  6. //            Version 1.0
  7. //
  8. //            (c) 1988-1992 Apple Computer, Inc. All Rights Reserved.
  9. //
  10. // ---------------------------------------------------------------------
  11.  
  12.  
  13. // Includes
  14. // --------
  15. #include <Windows.H>  // Required by Windows
  16. #include <WindowsX.H> // Required by Windows
  17.  
  18. #include <dos.h>    // Standard "C" header
  19. #include <errno.h>  // Standard "C" header
  20. #include <stdarg.h> // Standard "C" header
  21. #include <stdlib.h> // Standard "C" header
  22. #include <stdio.h>  // Standard "C" header
  23.  
  24. #include "Common.H" // Interface to Common.C
  25.  
  26.  
  27. // Globals
  28. // -------
  29. static struct
  30.    {char szOutOfMemory[COMMON_STRING_MAX]; // Out of memory message
  31.     WORD idNoMemMsg;                       // Non memory message id
  32.    } g;
  33.  
  34.  
  35. // Region Code Table
  36. // --------------------------------------------------------------------
  37. // Ordinal position represents corresponding Mac region code
  38. // --------------------------------------------------------------------
  39. static char* pszRegion[] = {
  40.    "enu", // 0
  41.    "fra", // 1
  42.    "eng", // 2
  43.    "deu", // 3
  44.    "ita", // 4
  45.    "nld", // 5
  46.    "fra", // 6
  47.    "sve", // 7
  48.    "esn", // 8
  49.    "dan", // 9
  50.    "ptg", // 10
  51.    "frc", // 11
  52.    "nor", // 12
  53.    "---", // 13
  54.    "---", // 14
  55.    "eng", // 15
  56.    "---", // 16
  57.    "fin", // 17
  58.    "fra", // 18
  59.    "deu", // 19
  60.    "---", // 20
  61.    "isl", // 21
  62.    ""     // Last entry must be NULL
  63.   };
  64.  
  65.  
  66. // Function: CommonAlloc - Allocate memory
  67. // --------------------------------------------------------------------
  68. // Parameters: LONG lBytes;            /* Number of bytes to allocate */
  69. //
  70. // Returns:    VOID FAR *pvMemory;     /* Memory block */
  71. // --------------------------------------------------------------------
  72. VOID FAR * FAR CommonAlloc (LONG lBytes)
  73.  
  74. // Perform simple allocation for now
  75.  
  76. {
  77.     HGLOBAL  hmem; // Memory handle
  78.  
  79.     if( hmem = GlobalAlloc( GHND, (DWORD) lBytes ))
  80.         return GlobalLock( hmem );
  81.     else
  82.         return NULL;
  83. }
  84.  
  85.  
  86. // Function: CommonFormatMessage - Format Message
  87. // --------------------------------------------------------------------
  88. // Parameters: HINSTANCE hResources;   /* Resource-only DLL handle */
  89. //             WORD idMsg;             /* ID of message in resource file */
  90. //             LPSTR lpszMsg;          /* Area in which to build message */
  91. //
  92. // Returns:    LPSTR lpszMsg;
  93. // --------------------------------------------------------------------
  94. LPSTR FAR CommonFormatMessage
  95.     (HINSTANCE hResources, WORD idMsg, LPSTR lpszMsg, ...)
  96.  
  97. // Define function data
  98.  
  99. {
  100.     char szFormat[COMMON_STRING_MAX]; // Message format
  101.     va_list pArgs;                    // String substitution arguments
  102.  
  103.     // Load the message string
  104.  
  105.     LoadString (hResources, idMsg, szFormat, sizeof (szFormat));
  106.  
  107.     // Build the message text
  108.  
  109.     va_start (pArgs, lpszMsg);
  110.     wvsprintf (lpszMsg, szFormat, (LPSTR) pArgs);
  111.     va_end (pArgs);
  112.  
  113.     // Return to caller
  114.  
  115.     return lpszMsg;
  116.  
  117. }
  118.  
  119.  
  120. // Function: CommonFree - Free memory allocated by CommonAlloc
  121. // --------------------------------------------------------------------
  122. // Parameters: VOID FAR *pvMemory;     /* Memory to free */
  123. //
  124. // Returns:    None
  125. // --------------------------------------------------------------------
  126. VOID FAR CommonFree (VOID FAR *pvMemory)
  127.  
  128. // Perform simple free for now
  129.  
  130. {
  131.     HGLOBAL   hmem; //Memory handle
  132.  
  133.     if( hmem = (HGLOBAL) LOWORD( GlobalHandle( SELECTOROF( pvMemory )))) {
  134.         GlobalUnlock( hmem );
  135.         GlobalFree( hmem );
  136.     }
  137.  
  138.     return;
  139. }
  140.  
  141.  
  142. // Function: CommonGetDirectoryOfModule - Get Path of Module
  143. // --------------------------------------------------------------------
  144. // Parameters: HINSTANCE hInstance;      /* Application's instance handle */
  145. //             LPSTR lpPath;             /* pointer to path buffer */
  146.  
  147. // Returns:    LPSTR lpPath;
  148. // --------------------------------------------------------------------
  149. LPSTR FAR CommonGetDirectoryOfModule (HINSTANCE hInstance, LPSTR lpPath)
  150.  
  151. {
  152.     char  szBuffer[CCHMAXPATH]; // Temporary buffer
  153.     LPSTR lpszTemp;             // Temporary pointer
  154.  
  155.     // Clear the buffer
  156.  
  157.     szBuffer[0] = '\0';
  158.  
  159.     // Get the fully qualified .EXE name
  160.  
  161.     if (!GetModuleFileName (hInstance, szBuffer, sizeof (szBuffer)))
  162.         return (LPSTR) NULL;
  163.  
  164.     // Look backwards until we find the last backslash
  165.  
  166.     lpszTemp = szBuffer + lstrlen(szBuffer);
  167.  
  168.     while ((lpszTemp > szBuffer) && (*lpszTemp != '\\'))
  169.         lpszTemp = AnsiPrev (szBuffer, lpszTemp);
  170.     if (*lpszTemp != '\\')
  171.         return (LPSTR) NULL;
  172.  
  173.     // We delete the last backslash; we now have the path specification
  174.  
  175.     *lpszTemp = '\0';
  176.     lstrcpy (lpPath, AnsiUpper (szBuffer));
  177.  
  178.     // Return to caller
  179.  
  180.     return lpPath;
  181.  
  182. }
  183.  
  184.  
  185. // Function: CommonGetLocalizedHelpFile - Get Help File Name
  186. // --------------------------------------------------------------------
  187. // Parameters: LPSTR lpszAppl;           /* Application's root name */
  188. //             LPSTR lpszName;           /* Area in which to build name */
  189. //             HINSTANCE hInstance;      /* Application's instance handle */
  190. //
  191. // Returns:    LPSTR pszName;
  192. // --------------------------------------------------------------------
  193. LPSTR FAR CommonGetLocalizedHelpFile
  194.     (LPSTR lpszAppl, LPSTR lpszName, HINSTANCE hInstance)
  195.  
  196. // Define function data
  197.  
  198. {
  199.     char   szBuffer[CCHMAXPATH];  // Temporary buffer
  200.     char   szApplDir[CCHMAXPATH]; // Application's directory
  201.     char   szLanguage[4];         // Language code
  202.     struct _find_t  fileinfo;     // Temp file info
  203.  
  204.     // Clear the area in which the name will be built
  205.  
  206.     lpszName[0] = '\0';
  207.  
  208.     // Get the application's directory
  209.  
  210.     if (!CommonGetDirectoryOfModule (hInstance, (LPSTR) szApplDir))
  211.         return lpszName;
  212.  
  213.     // Look for the localized help file in the ..\HELP directory
  214.  
  215.     GetProfileString( "intl", "sLanguage", "enu", szLanguage, sizeof(szLanguage));
  216.     wsprintf( szBuffer, "%s\\..\\help\\%s%s.hlp",
  217.         (LPSTR) szApplDir, (LPSTR) lpszAppl, (LPSTR) szLanguage );
  218.     if (_dos_findfirst (szBuffer, _A_NORMAL, &fileinfo) == 0) {
  219.         lstrcpy (lpszName, AnsiUpper (szBuffer));
  220.         return lpszName;
  221.     }
  222.  
  223.     // If it can't be found, look for the localized help
  224.     // file in the application's directory
  225.  
  226.     wsprintf( szBuffer, "%s\\%s%s.hlp",
  227.         (LPSTR) szApplDir, (LPSTR) lpszAppl, (LPSTR) szLanguage );
  228.     if (_dos_findfirst (szBuffer, _A_NORMAL, &fileinfo) == 0) {
  229.         lstrcpy (lpszName, AnsiUpper (szBuffer));
  230.         return lpszName;
  231.     }
  232.  
  233.     // If it can't be found, look for the US English help
  234.     // file in the ..\HELP directory
  235.  
  236.     wsprintf( szBuffer, "%s\\..\\help\\%senu.hlp",
  237.         (LPSTR) szApplDir, (LPSTR) lpszAppl );
  238.     if (_dos_findfirst (szBuffer, _A_NORMAL, &fileinfo) == 0) {
  239.         lstrcpy (lpszName, AnsiUpper (szBuffer));
  240.         return lpszName;
  241.     }
  242.  
  243.     // If it can't be found, look for the US English help
  244.     // file in the application's directory
  245.  
  246.     wsprintf( szBuffer, "%s\\%senu.hlp", (LPSTR) szApplDir, (LPSTR) lpszAppl );
  247.     if (_dos_findfirst (szBuffer, _A_NORMAL, &fileinfo) == 0) {
  248.         lstrcpy (lpszName, AnsiUpper (szBuffer));
  249.         return lpszName;
  250.     }
  251.  
  252.     // Otherwise, return NULL to the caller
  253.  
  254.     return lpszName;
  255.  
  256. }
  257.  
  258.  
  259. // Function: CommonGetLocalizedResources - Load Resource-Only DLL
  260. // --------------------------------------------------------------------
  261. // Parameters: LPSTR lpszAppl;         Application's root name
  262. //             HINSTANCE hInstance;    Application's instance handle
  263. //             WORD  idNoMemMsg;       id of no memory message
  264. //
  265. // Returns:    HINSTANCE hResource;    handle to resource instance
  266. // --------------------------------------------------------------------
  267. HINSTANCE FAR CommonGetLocalizedResources
  268.     (LPSTR lpszAppl, HINSTANCE hInstance, WORD idNoMemMsg )
  269.  
  270. // Define function data
  271.  
  272. {
  273.     char szBuffer[CCHMAXPATH]; // Temporary buffer
  274.     char szLanguage[4];        // Language code
  275.     LPSTR  lpszSave;           // Temporary pointer
  276.     HINSTANCE hResources;      // Resource handle
  277.  
  278.     // Suppress DLL load warnings
  279.  
  280.     SetErrorMode (SEM_NOOPENFILEERRORBOX);
  281.  
  282.     // Get the application path
  283.  
  284.     if (!CommonGetDirectoryOfModule (hInstance, (LPSTR) szBuffer))
  285.         return NULL;
  286.  
  287.     // Build the name of the resource-only DLL
  288.  
  289.     lstrcat (szBuffer, "\\");
  290.     lstrcat (szBuffer, lpszAppl);
  291.     lpszSave = szBuffer + lstrlen(szBuffer);
  292.     GetProfileString ("intl", "sLanguage", "enu", szLanguage, sizeof (szLanguage));
  293.     lstrcat (szBuffer, szLanguage);
  294.     lstrcat (szBuffer, ".dll");
  295.     AnsiUpper (szBuffer);
  296.  
  297.     // Load the resource-only DLL. If it can't be found, use the US
  298.     // English DLL. If it can't be found, we'll use the original .EXE
  299.     // resources
  300.  
  301.     if ((hResources = LoadLibrary (szBuffer)) == 0)
  302.         return NULL;
  303.     else if (hResources < (HINSTANCE) 32) {
  304.         lstrcpy (lpszSave, "enu");
  305.         lstrcat (lpszSave, ".dll");
  306.         AnsiUpper (szBuffer);
  307.         if ((hResources = LoadLibrary (szBuffer)) == 0)
  308.             return NULL;
  309.         if (hResources < (HINSTANCE) 32)
  310.             hResources = hInstance;
  311.     }
  312.  
  313.     // Load the out of memory message
  314.  
  315.     if (idNoMemMsg > 0) {
  316.         LoadString (hResources, idNoMemMsg, g.szOutOfMemory, sizeof (g.szOutOfMemory));
  317.         g.idNoMemMsg = idNoMemMsg;
  318.     }
  319.  
  320.     // Return to the caller
  321.  
  322.     return hResources;
  323.  
  324. }
  325.  
  326.  
  327. // Function: CommonGetCurrentRegion - Get Region Code for Current Language
  328. // --------------------------------------------------------------------
  329. // Parameters: none
  330. //
  331. // Returns:    UINT uiRegion;          Mac region code corresponding
  332. //                                     to current Windows language code
  333. // --------------------------------------------------------------------
  334. UINT FAR CommonGetCurrentRegion (VOID)
  335.  
  336. // Define function data
  337.  
  338. {
  339.     char   szLanguage[4]; // Language code
  340.     UINT   uiRegion;      // Region code
  341.  
  342.     // Match Windows language code to Mac region code
  343.  
  344.     GetProfileString( "intl", "sLanguage", "enu", szLanguage, sizeof(szLanguage));
  345.     for (uiRegion = 0; *pszRegion[uiRegion]; uiRegion++)
  346.         if (lstrcmpi (szLanguage, pszRegion[uiRegion]) == 0)
  347.             break;
  348.  
  349.     // Return to the caller
  350.  
  351.     return (*pszRegion[uiRegion])? uiRegion : 0;
  352.  
  353. }
  354.  
  355.  
  356. // Function: CommonTellUser - Issue message to User
  357. // --------------------------------------------------------------------
  358. // Parameters: HINSTANCE hResources;   /* Recource-only DLL handle */
  359. //             WORD idMsg;             /* ID of message in resource DLL */
  360. //             WORD idCaption;         /* ID of caption in resource DLL */
  361. //             WORD idStyle            /* MessageBox style flags */
  362. //
  363. // Returns:    int idReply;            /* MessageBox reply flags */
  364. // --------------------------------------------------------------------
  365. int FAR CommonTellUser
  366.   (HINSTANCE hResources, WORD idMsg, WORD idCaption, WORD idStyle, ...)
  367.  
  368. // Define function data
  369.  
  370. {
  371.     char szFormat[COMMON_STRING_MAX];  // Message format
  372.     char szBuffer[COMMON_STRING_MAX];  // Message buffer
  373.     char szCaption[COMMON_STRING_MAX]; // Message caption
  374.     WORD idReply;                      // Message response
  375.     va_list pArgs;                     // String substitution arguments
  376.  
  377.     // check for out of memory id
  378.  
  379.     if (g.idNoMemMsg == idMsg) {
  380.         idReply = MessageBox (NULL, g.szOutOfMemory, NULL, MB_OK | MB_SYSTEMMODAL | MB_ICONHAND );
  381.         goto endTellUser;
  382.     }
  383.  
  384.     // Load the caption string
  385.  
  386.     LoadString (hResources, idCaption, szCaption, sizeof (szCaption));
  387.  
  388.     // Load the message string
  389.  
  390.     LoadString (hResources, idMsg, szFormat, sizeof (szFormat));
  391.  
  392.     // Build the message text
  393.  
  394.     va_start (pArgs, idStyle);
  395.     wvsprintf (szBuffer, szFormat, (LPSTR) pArgs);
  396.     va_end (pArgs);
  397.  
  398.     // Display the message and capture response
  399.  
  400.     idReply = MessageBox (NULL, szBuffer, szCaption, idStyle);
  401.  
  402.  // Terminate if abort was requested
  403.  
  404.  endTellUser: 
  405.     if (idReply == IDABORT)
  406.         abort ();
  407.  
  408.     // Return reply
  409.  
  410.     return idReply;
  411.  
  412. }
  413.